package com.kmproject.haifu.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.base.DefaultAdapter;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.DataHelper;
import com.jess.arms.widget.alertview.AlertView;
import com.jess.arms.widget.alertview.AlertViewOnItemClickListener;
import com.jess.arms.widget.autolayout.AutoToolbar;
import com.kmproject.haifu.R;
import com.kmproject.haifu.app.AppConstant;
import com.kmproject.haifu.di.component.DaggerHousePublishSelectTypeComponent;
import com.kmproject.haifu.mvp.contract.HousePublishSelectTypeContract;
import com.kmproject.haifu.mvp.model.entity.beans.HousePayWayBean;
import com.kmproject.haifu.mvp.model.entity.beans.MemberCenterUserInfoBean;
import com.kmproject.haifu.mvp.model.entity.beans.MyStoryBean;
import com.kmproject.haifu.mvp.model.entity.beans.PublishNumCheakBean;
import com.kmproject.haifu.mvp.presenter.HousePublishSelectTypePresenter;
import com.kmproject.haifu.mvp.ui.adapter.HousePublishSelectTypeAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 06/26/2019 10:42
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
public class HousePublishSelectTypeActivity extends BaseActivity<HousePublishSelectTypePresenter> implements HousePublishSelectTypeContract.View {

    @BindView(R.id.toolbar_back)
    RelativeLayout toolbarBack;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.back_right_icon)
    ImageView backRightIcon;
    @BindView(R.id.back_right_txt)
    TextView backRightTxt;
    @BindView(R.id.toolbar_right)
    RelativeLayout toolbarRight;
    @BindView(R.id.toolbar)
    AutoToolbar toolbar;
    @BindView(R.id.toolbar_backImg)
    ImageView toolbarBackImg;
    @BindView(R.id.housePublise_selectType_RecyclerView)
    RecyclerView housePubliseSelectTypeRecyclerView;

    HousePublishSelectTypeAdapter adapter;

    MemberCenterUserInfoBean mmemberCenterUserInfoBean;
    PublishNumCheakBean mpublishNumCheakBean;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerHousePublishSelectTypeComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_house_publish_select_type; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
//        MyStoryBeanResultBean

     ArmsUtils.configRecyclerView(housePubliseSelectTypeRecyclerView,new LinearLayoutManager(this));
//        adapter=new HousePublishSelectTypeAdapter(mmemberCenterUserInfoBean,getIntent().hasExtra("MyStoryBeanResultBean")?((MyStoryBean.ResultBean)getIntent().getSerializableExtra("MyStoryBeanResultBean")):null,new ArrayList<>());
        MyStoryBean.ResultBean   mresultBean= getIntent().hasExtra("MyStoryBeanResultBean") ? (MyStoryBean.ResultBean) getIntent().getSerializableExtra("MyStoryBeanResultBean") : null;

        adapter=new HousePublishSelectTypeAdapter(new ArrayList<>(), new HousePublishSelectTypeAdapter.ItemCallBack() {
            @Override
            public void callBack(HousePayWayBean housePayWayBean,int childIndex) {
                if (mpublishNumCheakBean==null)
                {
                    ArmsUtils.makeText(HousePublishSelectTypeActivity.this,"未获取到配置参数");
                    return;
                }
                if (mpublishNumCheakBean.getHouseCount()==0)
                {
                    new AlertView("", "您发布的房源以达到当前会员等级可发布房源数量上限，如需继续发布房源，请先升级您的会员等级！", "我知道了", new String[]{"去升级会员"}, null, HousePublishSelectTypeActivity.this, AlertView.Style.Alert, new AlertViewOnItemClickListener() {

                        @Override
                        public void onItemClick(Object o, int position) {
                            if (position==AlertView.CANCELPOSITION)
                            {

                            }else {
                            Intent intent = new Intent(HousePublishSelectTypeActivity.this, MemberCenterMainActivity.class);
                            intent.putExtra("type","house_updateMember");//从房屋跳转到升级会员界面
                            ArmsUtils.startActivityForResult(intent, 1002);
                            }

                        }
                    },ArmsUtils.getColor(HousePublishSelectTypeActivity.this,R.color.alert_button_00adfcr),true).show();

                    return;
                }
                if ("jmzz".equals(housePayWayBean.getCategory()))
                {
                    Intent intent=new Intent(HousePublishSelectTypeActivity.this, HousePublishDuanZuPublishActivity.class);
                    intent.putExtra("type",housePayWayBean.getChildren().get(childIndex).getCategory());
                    intent.putExtra("HousePayWayBean",housePayWayBean.getChildren().get(childIndex));
                    if (mresultBean!=null)
                    {
                        intent.putExtra("MyStoryBeanResultBean",mresultBean);
                    }
                    ArmsUtils.startActivityForResult(intent,1001);
                }else if ("sp".equals(housePayWayBean.getChildren().get(childIndex).getCategory())){
                    //商铺
                    Intent intent1=new Intent(HousePublishSelectTypeActivity.this, HousePublishStorePublishActivity.class);
                    intent1.putExtra("HousePayWayBean",housePayWayBean.getChildren().get(childIndex));
                    intent1.putExtra("type",housePayWayBean.getChildren().get(childIndex).getCategory());
                    if (mresultBean!=null)
                    {
                        intent1.putExtra("MyStoryBeanResultBean",mresultBean);
                    }
                    ArmsUtils.startActivityForResult(intent1,1001);
                }else {
                    //车位写字楼
                    Intent intent1=new Intent(HousePublishSelectTypeActivity.this, HousePublishOfficePublishActivity.class);
                    intent1.putExtra("HousePayWayBean",housePayWayBean.getChildren().get(childIndex));
                    intent1.putExtra("type",housePayWayBean.getChildren().get(childIndex).getCategory());
                    if (mresultBean!=null)
                    {
                        intent1.putExtra("MyStoryBeanResultBean",mresultBean);
                    }
                    ArmsUtils.startActivityForResult(intent1,1001);
                }
            }
        });
        housePubliseSelectTypeRecyclerView.setAdapter(adapter);
        adapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_RIGHT);
        mPresenter.dictList();


    }

    @Override
    protected void onResume() {
        super.onResume();
//        mPresenter.getMemberLevelByUser();
        mPresenter.isPublishCount();
    }

    @Override
    public void showLoading() {
        loadingDialog.show();
    }

    @Override
    public void hideLoading() {
        loadingDialog.dismiss();
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }



    @Override
    public void dictListSuccess(List<HousePayWayBean> housePayWayBeans) {
        adapter.setNewData(housePayWayBeans);
    }

    @Override
    public void getMemberLevelByUserSuccess(MemberCenterUserInfoBean memberCenterUserInfoBean) {
        if (memberCenterUserInfoBean == null) {
            return;
        }
        mmemberCenterUserInfoBean=memberCenterUserInfoBean;
        DataHelper.saveDeviceData(this, AppConstant.user_memberInfo, memberCenterUserInfoBean);

    }

    @Override
    public void isPublishCountSuccess(PublishNumCheakBean publishNumCheakBean) {
        mpublishNumCheakBean=publishNumCheakBean;
    }

    @Override
    protected void onDestroy() {
        DefaultAdapter.releaseAllHolder(housePubliseSelectTypeRecyclerView);
        super.onDestroy();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==1001&&resultCode==RESULT_OK&&data!=null)
        {
            if (data.hasExtra("isGoToShow"))
            {
                //去个人中心
                Intent  intent = new Intent(HousePublishSelectTypeActivity.this, MyCollectionActivity.class);
                intent.putExtra("type", 1);
                ArmsUtils.startActivity(intent);
                killMyself();
            }

        }
    }
}
